/*
   tsAPI Distribution
   ts_ScrollBar Class

   TrueSpectra Inc, 2002
   Code by: Kevin Martin
*/

function ts_ScrollBar( orientation, wid, hei, watchmouseOver, imgFolder ) {
	this.DynLayer = DynLayer;
        this.DynLayer(null,0,0,wid,hei);

        this.knobsz = 30;
	this.or = orientation;

	this.imgFolder	= imgFolder || DynAPI.librarypath+"tsapi/images/default/";
	if( this.imgFolder.substring(this.imgFolder.length-1,this.imgFolder.length) != "/" ) this.imgFolder+="/";

	this.imgback_m = this.addChild( new DynImage( getTSGif(DynAPI.librarypath+'tsapi/images/spin_bar_m.tsgif') ) );
	this.imgback_l = this.addChild( new DynImage( getTSGif(DynAPI.librarypath+'tsapi/images/spin_bar_l.tsgif') ) );
	this.imgback_r = this.addChild( new DynImage( getTSGif(DynAPI.librarypath+'tsapi/images/spin_bar_r.tsgif') ) );

	this.knob_size = new Array(wid,hei);
	if( this.or == ts_ScrollBar.HORIZONTAL) this.knob_size[0] = this.knobsz;  
	else 					this.knob_size[1] = this.knobsz;

	this.knob = this.addChild(new DynLayer(null,0,0,this.knob_size[0],this.knob_size[1]));

	this.knob.img_m = this.knob.addChild( new DynImage( getTSGif(this.imgFolder+'slider_'+(this.or == ts_ScrollBar.HORIZONTAL?'h':'v')+'.tsgif')) );

	if( watchmouseOver == true ) {
		this.mouseEvents = new EventListener(this);
		this.mouseEvents.onmousemove = function(e) {
			var knob = e.getTarget().knob;
			knob.moveTo(e.x,knob.y);
			e.getTarget().invokeEvent("scroll");
		};
		this.mouseEvents.onmouseout = function(e) {
			e.getTarget.invokeEvent("scrollStop");
		};
		this.addEventListener(this.mouseEvents);
	} else {
		this.dragEvents = new EventListener(this);
		this.dragEvents.ondragend=function(e)  { e.getTarget().invokeEvent("scrollStop"); };
		this.dragEvents.ondragmove=function(e) { e.getTarget().invokeEvent("scroll");     };
		DragEvent.setDragBoundary(this.knob);
		DragEvent.enableDragEvents(this.knob);
		this.knob.addEventListener(this.dragEvents);
	};

	var resizeEvents = new EventListener(this);
	resizeEvents.onresize = function(e) {
		var o = e.getTarget();

		if( orientation == ts_ScrollBar.HORIZONTAL) {
			o.setBgImage( getTSGif(DynAPI.librarypath+'tsapi/images/spin_bar_m.tsgif'));
			o.imgback_l.moveTo(0,0);
			o.imgback_m.moveTo(0,0);
			o.imgback_r.moveTo(o.w-3,0);
			o.imgback_l.setSize(3,o.h);
			o.imgback_m.setSize(o.w,o.h);
			o.imgback_r.setSize(3,o.h);
			o.knob_size[1] = o.h;
		} else {
			o.knob_size[0] = o.w;
		};

		o.knob.setSize( o.knob_size[0], o.knob_size[1] );
		o.knob.img_m.setSize( o.knob_size[0], o.knob_size[1] );

		if( o.getPos() > o.getMaxPos() ) o.setPos( Math.max(0,o.getMaxPos()) );

	}; 
	this.addEventListener(resizeEvents);

	this.setOrientation(orientation);
};

ts_ScrollBar.prototype = new DynLayer;

ts_ScrollBar.prototype.getMaxPos = function() {
	if( this.or == ts_ScrollBar.HORIZONTAL )	return (this.w-this.knob_size[0]);
	else						return (this.h-this.knob_size[1]);
};

ts_ScrollBar.prototype.setPos = function(i) {
	i = Math.max(0,Math.min(this.getMaxPos(),i));
	if( this.or == ts_ScrollBar.HORIZONTAL )	this.knob.moveTo( i, 0 );
	else						this.knob.moveTo( 0, i );
};

ts_ScrollBar.prototype.getPos = function(i) {
	if( this.or == ts_ScrollBar.HORIZONTAL )	return this.knob.x;
	else						return this.knob.y;
};

ts_ScrollBar.prototype.getPcnt = function(i) {
	return (this.getPos()/this.getMaxPos());
};

ts_ScrollBar.prototype.setOrientation = function(type) { // 0=horz,1=vert
	this.horizontal = (type==ts_ScrollBar.HORIZONTAL);
	this.vertical = (type==ts_ScrollBar.VERTICAL);
};


ts_ScrollBar.prototype.reset = function() {
	this.setPos(0);
};

ts_ScrollBar.prototype.changeUI = function(imgFolder) {
	this.imgFolder	= imgFolder || DynAPI.librarypath+"tsapi/images/default/";
	if( this.imgFolder.substring(this.imgFolder.length-1,this.imgFolder.length) != "/" ) this.imgFolder+="/";

	var newimg = DynImage.getImage( getTSGif(this.imgFolder+'slider_'+(this.or == ts_ScrollBar.HORIZONTAL?'h':'v')+'.tsgif') );
	this.knob.img_m.setImage(newimg);
};

ts_ScrollBar.VERTICAL = 1;
ts_ScrollBar.HORIZONTAL = 2;

function getTSGif(s) {
  return s;
};